<?php defined("MAIN_DIR") or die("No direct access");

if ($_FILES["Filedata"]) {

	$src = $_FILES["Filedata"]["tmp_name"];
	$size = getimagesize($src);

	list($type,$ext) = explode("/",$size['mime']);

	if ($type != "image"){
		die;
	}

	$f_name = ($ext == "jpg") ? "jpeg" : $ext;
	$icf = "imagecreatefrom".$f_name;

	if (!function_exists($icf))
		die;

	$source = $icf($src);
	
	$path = APP."/files/img/photos/";
	$filename = time().rand(10,99).".jpg";

	$result = resizeImg($src,910,400,$path."/x_{$filename}",$icf);
	if ($result === true){
		resizeImg($src,250,109,$path."/z_{$filename}",$icf);

		$order = $db->getCell("select `order`+1 from `photos` order by `order` desc limit 1");
		$order = $order ? $order : 0;
		$img_id = $db->sendQuery("insert into `photos` values (NULL,'$filename','','0','$order')");
		
		echo "{\"id\":\"{$img_id}\",";
		echo "\"filename\":\"{$filename}\",";
		echo "\"big\":\"/files/img/photos/x_{$filename}\",";
		echo "\"small\":\"/files/img/photos/z_{$filename}\"}";
	} else {
		echo "{\"error\":\"$result\"}";
	}
}

?>